import { Destroyable, react, reactJson, Event, track, HasOwner, reactJsonWithUndefined } from "xbsj-xe2/dist-node/xe2-base-utils";
import { MyProjectManager } from "../MyProjectManager";
import { ES3DTileset, ESImageryLayer, ESTerrainLayer, ESTextLabel } from "esobjs-xe2-plugin/dist-node/esobjs-xe2-plugin-main";
import { ElMessage } from "element-plus";


export class SwitchEngine extends HasOwner<MyProjectManager> {

    // 显示信令服务器输入
    private _showESSSDialog = this.dv(react<boolean>(false));
    get showESSSDialog() { return this._showESSSDialog.value; }
    get showESSSDialogChanged() { return this._showESSSDialog.changed; }
    set showESSSDialog(value: boolean) { this._showESSSDialog.value = value; }

    // 信令服务器服务地址
    // private _uri = "http://localhost:9007/"
    // get uri() { return this._uri; }

    // 实例id
    private _uri = this.dv(react<string>("http://localhost:9007/"));
    get uri() { return this._uri.value; }
    get uriChanged() { return this._uri.changed; }
    set uri(value: string) { this._uri.value = value; }

    // 应用列表
    private _appLists = this.dv(react<any>([]));
    get appLists() { return this._appLists.value; }
    get appListsChanged() { return this._appLists.changed; }
    set appLists(value: any) { this._appLists.value = value; }

    private _status = this.dv(react<boolean>(false));
    get status() { return this._status.value; }
    get statusChanged() { return this._status.changed; }
    set status(value: boolean) { this._status.value = value; }

    // 实例id
    private _appId = this.dv(react<string>(""));
    get appId() { return this._appId.value; }
    get appIdChanged() { return this._appId.changed; }
    set appId(value: string) { this._appId.value = value; }

    // dom
    private _domId = this.dv(react<any>(""));
    get domId() { return this._domId.value; }
    get domIdChanged() { return this._domId.changed; }
    set domId(value: string) { this._domId.value = value; }

    // 切换引擎弹出框
    private _switchEngineDialog = this.dv(react<boolean>(false));
    get switchEngineDialog() { return this._switchEngineDialog.value; }
    get switchEngineDialogChanged() { return this._switchEngineDialog.changed; }
    set switchEngineDialog(value: boolean) { this._switchEngineDialog.value = value; }

    // 切换引擎弹出框事件
    private _switchEngineDialogEvent = this.dv(new Event<[string]>());
    get switchEngineDialogEvent() { return this._switchEngineDialogEvent; }

    // 弹出框事件
    private _dialogEvent = this.dv(new Event<[string]>());
    get dialogEvent() { return this._dialogEvent; }

    switchCesium() {
        // 切换Cesium的API
        if (!this.domId) {
            alert("未获取到对应的dom元素")
            return
        }
        const c = this.owner.cimrtsSceneTreeWrapper?.json.root.children
        if (c && c.length) {
            return new Promise((resolve) => {
                this.switchEngineDialog = true
                const d = this._switchEngineDialogEvent.disposableOnce((res: string) => {
                    d()
                    if (res == "confirm") {
                        this.owner.cimrtsSceneTreeWrapper?.destroyAllSceneObjectTreeItems()
                        this.owner.switchToCesiumViewer({
                            "domid": this.domId
                        })
                    }
                    this.switchEngineDialog = false
                    resolve("ok")
                });
            })

        } else {
            this.owner.switchToCesiumViewer({
                "domid": this.domId
            })
        }

    }
    switchUE() {
        return new Promise((resolve1) => {
            this.init()
            this.showESSSDialog = true
            const d = this._dialogEvent.disposableOnce((res: string) => {
                d()
                if (res == "confirm") {

                    if (!this.domId) {
                        ElMessage.error("未获取到对应的dom元素")
                        return
                    }
                    if (!this.uri) {
                        ElMessage.error("请填写ESSS信令服务器接口服务地址参数uri")
                        return
                    }
                    if (!this.appId) {
                        ElMessage.error("请填写实例id参数app")
                        return
                    }

                    const c = this.owner.cimrtsSceneTreeWrapper?.json.root.children
                    if (c && c.length) {
                        return new Promise((resolve2) => {
                            this.switchEngineDialog = true
                            const d = this._switchEngineDialogEvent.disposableOnce((res: string) => {
                                d()
                                if (res == "confirm") {
                                    this.owner.cimrtsSceneTreeWrapper?.destroyAllSceneObjectTreeItems()
                                    this.owner.switchToUEViewer({
                                        "domid": this.domId,
                                        "uri": this.uri,
                                        "app": this.appId
                                    })
                                }
                                this.showESSSDialog = false
                                this.switchEngineDialog = false
                                resolve2("ok")
                            });
                        })

                    } else {
                        // 切换到UE视口API
                        this.owner.switchToUEViewer({
                            "domid": this.domId,
                            "uri": this.uri,
                            "app": this.appId
                        })
                    }

                }
                this.showESSSDialog = false
                resolve1("ok")
            });
        })
    }

    init() {
        try {
            fetch(`${this.uri}ue/app`).then(response => response.text()).then((value) => {
                this.status = true
                this.appLists = JSON.parse(value).data
            }).catch(res => {
                this.appLists = []
                this.appId = ""
                this.status = false
            })
        } catch (error) {

        }
    }

    clear() {
        // this.uri = "http://localhost:9007/"
        this.appLists = []
        this.appId = ""
        this.status = false
        this.showESSSDialog = false
    }

    constructor(owner: MyProjectManager) {
        super(owner);


    }

}















